package board;

import character.Character;
import item.Item;
import com.jme3.scene.Node;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import misc.Vector3i;

public class Board extends Node {
    private Map<Vector3i, Tile> board = new HashMap<Vector3i, Tile>();
    private Map<Tile, Set<Character>> movementPaths = new HashMap<Tile, Set<Character>>();
    //private Set<Character> characters = new HashSet<Character>();
    //private Set<Item> items = new HashSet<Item>();
    
    @Deprecated
    public Board() {}

    public Board(int x, int z) {
        Vector3i currentLoc;
	for(int i=0; i<x; i++) {
	    for(int j=0; j<z; j++) {
                currentLoc = new Vector3i(i,0,j);
		Tile tile = new Tile();
		tile.setLocalTranslation(i*Tile.SIZE+Tile.SIZE/2, 0, j*Tile.SIZE+Tile.SIZE/2);
		tile.setBoardLocation(currentLoc);
		board.put(currentLoc, tile);
		this.attachChild(tile);
	    }
	}
    }

    public Tile getTile(Vector3i location) {
        return board.get(location);
    }
    
    public void addCharacter(Character character) {
        attachChild(character);
        //characters.add(character);
        board.get(character.getBoardLocation()).setCharacter(character);
    }

    public void addItem(Item item) {
	attachChild(item);
	//items.add(item);
        board.get(item.getBoardLocation()).setItem(item);
    }
    
    public void characterMoved(Character character, Vector3i oldLocation) {
        board.get(oldLocation).removeCharacter();
        Tile newTile = board.get(character.getBoardLocation());
        newTile.setCharacter(character);
        if(newTile.getItem() != null) {
            character.getInventory().addItem(newTile.getItem());
            detachChild(newTile.getItem());
            System.out.println(character+" picked up "+newTile.getItem());
            newTile.removeItem();
        }
    }
    
    public void addMovementPath(Tile tile, Character character) {
        if(!movementPaths.containsKey(tile)) {
            movementPaths.put(tile, new HashSet<Character>());
            tile.highlight();
        }
        movementPaths.get(tile).add(character);
    }
    
    public void removeMovementPath(Tile tile, Character character) {
        if(movementPaths.containsKey(tile)) {
            movementPaths.get(tile).remove(character);
            if(movementPaths.get(tile).isEmpty()) {
                movementPaths.remove(tile);
                tile.unhighlight();
            }
        }
    }
}
